import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_wan_android/arch/ui/loading.dart';
import 'package:flutter_wan_android/routes/route_util.dart';
import 'package:flutter_wan_android/widget/wan_appbar.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_plus/webview_flutter_plus.dart';

class WebPage extends StatefulWidget {
  @override
  _WebPageState createState() => _WebPageState();
}

class _WebPageState extends State<WebPage> {
  @override
  void initState() {
    super.initState();
    // Enable hybrid composition.
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  void dispose() {
    super.dispose();
    LoadingUtil.hide();
  }

  @override
  Widget build(BuildContext context) {

    //获取路由参数
    var params = RouteUtil.getParams(context);
    String url = params["url"];
    String title = params["title"];

    return Scaffold(
      appBar: WanAppbar(context, titleStr: title),
      body: WebViewPlus(
        initialUrl: url,
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (controller){
          LoadingUtil.show();
        },

        onProgress: (progress){
          if(progress > 70)  LoadingUtil.hide();
        },

        onPageFinished: (_){
          LoadingUtil.hide();
        },
      ),
    );
  }
}
